<%@page import="cn.sendto.mapper.StudentMapper"%>
<%@page import="cn.sendto.mapper.IRowMapper"%>
<%@page import="java.util.List"%>
<%@page import="cn.sendto.model.StudentEntity"%>
<%@page import="cn.sendto.dbhelper.DBHelper"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	
<%
      request.setCharacterEncoding("utf-8");
      String username=request.getParameter("username");
      String password=request.getParameter("password");
      if(username.isEmpty()){
    	  request.getSession().setAttribute("message", "请输入账户");
    	  response.sendRedirect("login.jsp");
    	  //request.setAttribute("message", "请输入账户");
    	  //request.getRequestDispatcher("login.jsp").forward(request, response);
      } else if(password.isEmpty()){
    	  request.getSession().setAttribute("message", "请输入密码");
    	  response.sendRedirect("login.jsp");
      } else{
    	  DBHelper helper=new DBHelper();
    	  IRowMapper mapper=new StudentMapper();
    	  final String QUERY="select * from studentinfo where studentname=?";
    	  List<StudentEntity> list=helper.executeQuery(QUERY,mapper,username);
    	  if(list.size()==0){
    		  request.getSession().setAttribute("message", "用户名不存在");
        	  response.sendRedirect("login.jsp");
    	  }else{
    		  if(password.equals(list.get(0).getPassword())){
    			  request.getSession().setAttribute("user", username);
    	    	  response.sendRedirect("main.jsp");
    			  //request.setAttribute("user", username);
        		  //request.getRequestDispatcher("main.jsp").forward(request, response);
    		  }else{
    			  request.getSession().setAttribute("message", "用户名或密码错误");
    	    	  response.sendRedirect("login.jsp");
    			  //request.setAttribute("message", "用户名或密码错误");
    			 // request.getRequestDispatcher("login.jsp").forward(request, response);
    		  }
    	  }
    	  helper.close();
      }
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
</html>